package com.cloud.www.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.cloud.www.model.entity.BArticle;
//import com.cloud.www.model.enums.ScrollChartEnum;
//import com.cloud.www.service.BArticleService;
import com.cloud.www.model.domain.HomeListDomain;
import com.cloud.www.model.entity.MFilm;
import com.cloud.www.model.entity.MType;
import com.cloud.www.service.MFilmService;
import com.cloud.www.service.MTypeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Controller
@AllArgsConstructor
@RequestMapping("")
public class HomeController extends BaseController {

    private final MFilmService mFilmService;
    private final MTypeService mTypeService;

    /**
     * 首页
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        StringBuilder sb = new StringBuilder();

        List<MType> listType = mTypeService.selectAll(new MType().setPid(0));
        for (MType mType : listType) {
            Page p = new Page();
            p.setCurrent(1);
            p.setSize(12);
            p.setDesc("id");
            IPage<MFilm> mFilmIPage = mFilmService.selectPage(p, new MFilm().setOneTypeId(mType.getId()));
            List<MFilm> list = mFilmIPage.getRecords();
            sb.append(" <h3 id=\"a1\"><i class=\"iconfont\"></i>" + mType.getTypeName() + "<span> <a href=\"/list/o_" + mType.getId() + ".html\">更多</a></span></h3>\n" +
                    "        <ul class=\"qrcode\">");
            for (MFilm mFilm : list) {

                sb.append("<li><a href=\"/info/" + mFilm.getId() + ".html\"><h2>" + mFilm.getName() + "</h2>\n" +
                        "                <div class=\"logo\"><img class=\"lazy\"\n" +
                        " data-original=\"" + mFilm.getImg() + "\"" +
                        "            alt=\""+mFilm.getName()+"\"></div>\n" +
                        "                <p class=\"desc\">更新："+ DateUtil.format(mFilm.getUpdateTime(), DatePattern.NORM_DATE_PATTERN) +"</p>\n" +
                        "            </a>\n" +
                        "            </li>");
            }

            sb.append("</ul>");

        }

        ModelAndView view = baseView("成长社区_Java|技术文章|开源代码|拼多多联盟", "home/index");
        view.addObject("listStr",sb.toString());
        return view;
    }

}
